import {
  uSelectDevShareData,
  uSelectMyShareData,
  uSelectMyShareLinkDev
} from '../../../api/setting'
import {observable, computed, action} from 'mobx'
import {createContext} from 'react'
import ChildrenPageUtils from '@/store/extends'
import {getStorage} from '@/utils/localstoreage'
import Taro from '@tarojs/taro'

class Singlebedevsharedlist extends ChildrenPageUtils {
  @observable pageQuery: {devId: string; devName: string} = {
    devId: '',
    devName: ''
  }

  @observable current: 1 | 2 = 1
  @action.bound
  changeCurrent(num: 1 | 2) {
    this.current = num
    this.updateDevList()
  }

  @observable isMydev: 0 | 1 = 1
  @action.bound
  changeIsMyDev(target: 0 | 1) {
    this.isMydev = target
  }

  @observable dataList: Setting.ShareData[] = []
  @observable detailData: Setting.MyShareData = {
    shareCount: 0,
    shareLinkDevCount: 0,
    passiveShareCount: 0
  }
  @action.bound
  async updateDevList() {
    let userId = getStorage('userId')
    if (!userId) return
    Taro.showLoading({
      title: '加载中...'
    })
    let res = await uSelectMyShareData({devId: this.pageQuery.devId, userId})
    if (res && res.result === 'success') {
      this.detailData = res.data
    } else {
      this.detailData = {
        shareCount: 0,
        shareLinkDevCount: 0,
        passiveShareCount: 0
      }
    }
    this.dataList = []
    if (this.current === 1) {
      let res1 = await uSelectDevShareData({
        devId: this.pageQuery.devId,
        userId
      })
      if (res1 && res1.result === 'success') {
        this.dataList = res1.data.shareDevMsgPackList
        this.isMydev = res1.data.isMyDev
      } else {
        this.dataList = []
      }
    } else if (this.current === 2) {
      let res1 = await uSelectMyShareLinkDev({userId})
      if (res1 && res1.result === 'success') {
        this.dataList = res1.data
      } else {
        this.dataList = []
      }
    }
    Taro.hideLoading()
  }
}
export const singleDevBeSharedListStore = createContext(
  new Singlebedevsharedlist()
)
